package com.ebelter.temperaturegun.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;


import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.utils.DipUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.temperaturegun.R;


public class TemperatureView extends View {

    private static final String TAG = "TemperatureView";
    public TemperatureView(Context context) {
        this(context,null);
    }

    public TemperatureView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TemperatureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }




    private Paint mPaint;
    private int mWidth;//控件的宽度
    private int mHeight;//控件的高度

    private Bitmap wenduXsjBitmap;
    private Rect srcWenduXsjBitmap;
    private Rect destWenduXsjBitmap;

    private void initBase() {
        mWidth=getMeasuredWidth();
        mHeight=getMeasuredHeight();
    }

    public void setDisplayTemperatureValue(float displayTemperatureValue) {
        this.displayTemperatureValue = displayTemperatureValue;
    }

    public float getDisplayTemperatureValue() {
        return displayTemperatureValue;
    }

    private float minTemperatureValue = 33.5f;//整个条的最小温度
    private float maxTemperatureValue = 43f;//整个条的最大温度
    private float displayTemperatureValueMin = 36.1f;
    private float displayTemperatureValueMax = 37.3f;

    private float displayTemperatureValueMin_F = 96.98f;
    private float displayTemperatureValueMax_F = 99.14f;

    private float displayTemperatureValue = 37;//小三角中显示的温度

    private boolean isDisplayValueSjx = true;

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {


        mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TemperatureView, defStyleAttr, 0);
        int count = array.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = array.getIndex(i);
            if (attr==R.styleable.TemperatureView_temperatureValue){
                displayTemperatureValue = array.getFloat(attr, -1);
            }

        }
        array.recycle();

        wenduXsjBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_wendu_xsj);
        srcWenduXsjBitmap = new Rect(0, 0,wenduXsjBitmap.getWidth() , wenduXsjBitmap.getHeight());
        LogUtils.i(TAG,"----wenduXsjBitmap.getWidth()="+wenduXsjBitmap.getWidth()+"----wenduXsjBitmap.getHeight()="+wenduXsjBitmap.getHeight());


    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
            //这里需要确定几个基本点，只有确定了xy轴原点坐标，第一个点的X坐标值及其最大最小值
            initBase();
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);



        drawBaseText(canvas);
        drawPic(canvas);


    }




    private int blueColor = Color.parseColor("#3870EE");
    private int greenColor = Color.parseColor("#629C36");
    private int yellowColor = Color.parseColor("#EFC355");

    private void drawPic(Canvas canvas) {


        mPaint.setStrokeWidth(30);
        mPaint.setStrokeCap(Paint.Cap.ROUND);

        float p1 = 0.70f;
        mPaint.setColor(blueColor);
        canvas.drawLine(0+dp1*5,mHeight*p1,mWidth/2,mHeight*p1,mPaint);

        mPaint.setColor(yellowColor);
        canvas.drawLine(mWidth/2,mHeight*p1,mWidth-dp1*5,mHeight*p1,mPaint);

        mPaint.setColor(greenColor);
        mPaint.setStrokeCap(Paint.Cap.SQUARE);
        canvas.drawLine(mWidth/3,mHeight*p1,mWidth*2/3,mHeight*p1,mPaint);


        if (isDisplayValueSjx){

            float left;

            LogUtils.i(TAG,"mWidth = "+mWidth);

            int left1 ;
            if (displayTemperatureValue<=displayTemperatureValueMin){

                float wp1 = 1;
                if (displayTemperatureValue!=displayTemperatureValueMin){
                    wp1=(displayTemperatureValue-minTemperatureValue)/(displayTemperatureValueMin-minTemperatureValue);
                }
                left=(mWidth/3)*wp1;
                left1 = (int)( left-wenduXsjBitmap.getWidth()/2-13.5);
                left1=0;
                LogUtils.i(TAG,"wp1="+wp1+"--left="+left);


            }else if (displayTemperatureValue>displayTemperatureValueMin&&displayTemperatureValue<=displayTemperatureValueMax){

                float wp2 = 1;
                if (displayTemperatureValueMin!=displayTemperatureValueMax){
                    wp2=(displayTemperatureValue-displayTemperatureValueMin)/(displayTemperatureValueMax-displayTemperatureValueMin);
                }
                left=(mWidth/3)*(wp2+1);
                LogUtils.i(TAG,"wp2="+wp2+"--left="+left);
                left1 = (int)( left-wenduXsjBitmap.getWidth()/2+dp1*5);



            }else {

                float wp3 = 1;
                if (displayTemperatureValueMax!=maxTemperatureValue){
                    wp3=(displayTemperatureValue-displayTemperatureValueMin)/(maxTemperatureValue-displayTemperatureValueMax);
                }
                left=(mWidth/3)*(wp3+2);
                left1 = (int)( left-wenduXsjBitmap.getWidth()/2+dp1*5);
                left1=mWidth-wenduXsjBitmap.getWidth();
                LogUtils.i(TAG,"wp3="+wp3+"--left="+left);
            }




            int top = (int) (mHeight*0.7-40-wenduXsjBitmap.getHeight());
            int right = wenduXsjBitmap.getWidth();
            int bottom = wenduXsjBitmap.getHeight();

            LogUtils.i(TAG,"left1="+left1+"--top="+top+"--right="+right+"--bottom="+bottom);

            destWenduXsjBitmap = new Rect(left1,top,left1+right,top+bottom);
            canvas.drawBitmap(wenduXsjBitmap,srcWenduXsjBitmap,destWenduXsjBitmap,mPaint);



        }









    }



    float valueTextSize = DipUtil.dip2px(getContext(),15f);
    float dp1 = DipUtil.dip2px(getContext(),1);
    private void drawBaseText(Canvas canvas) {
        mPaint.setColor(Color.parseColor("#ffffff"));
        mPaint.setTextSize(valueTextSize);

        String piandi = CommonLib.getString(R.string.piandi);
        canvas.drawText(piandi,(mWidth/3-DipUtil.getTextWidth(mPaint,piandi))/2,mHeight-dp1*2,mPaint);

        String zhengchang = CommonLib.getString(R.string.zhengchang);
        canvas.drawText(zhengchang,(mWidth-DipUtil.getTextWidth(mPaint,zhengchang))/2,mHeight-dp1*2,mPaint);

        String piangao = CommonLib.getString(R.string.piangao);
        canvas.drawText(piangao,mWidth*5/6-DipUtil.getTextWidth(mPaint,piangao)/2,mHeight-dp1*2,mPaint);

        String tip1;
        String tip2;

        if (unitModel==0){
            //C
            tip1=displayTemperatureValueMin+"";
            tip2 = displayTemperatureValueMax+"";
        }else {
            //F
            tip1=displayTemperatureValueMin_F+"";
            tip2=displayTemperatureValueMax_F+"";
        }

        canvas.drawText(tip1,mWidth/3-DipUtil.getTextWidth(mPaint,tip1)/2,DipUtil.getTextHeight(mPaint,tip1)+dp1*2,mPaint);


        canvas.drawText(tip2,mWidth*2/3-DipUtil.getTextWidth(mPaint,tip2)/2,DipUtil.getTextHeight(mPaint,tip1)+dp1*2,mPaint);
    }

    private int unitModel;//0代表°C 1代表°F
    public void setUnitModel(int unitModel){
        this.unitModel=unitModel;
    }
}
